package com.hikari;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


/*
    1-1 窗口的创建
    1-2 创建一个窗口类，继承JFrame类，成为窗口类，这个类实现窗口的自动绘制
*/
public class GameWin extends JFrame {
    //成员变量
    int width = 1024, height = 768;
    double random;
    //计数器
    int time = 0;
    //3.启动页面的制作
    //3-1游戏状态5种：0 未开始 1 游戏中 2 通关失败 3 通关成功  4 暂停
    //3-2定义游戏状态
    public static int state = 0;
    //5-3 在窗口类中获取背景类对象
    Bg bg = new Bg();
    //模块6：双缓存解决闪屏问题，创建一个空的图片，把所有的组件绘制到空的图片上，将绘制好的图片添加到主窗口上
    //6-1 定义一个成员变量
    Image offImage;
    //7 敌方第一条鱼的添加
    Enemy enemy = new Enemy.Enemy_1_r();
    MyFish myFish = new MyFish();
    Enemy boss;
    boolean isBoss=false;

    /*1-3 创建一个启动方法，用来设置窗口信息*/
    public void launch() {
        //1-4 窗口是否可见
        this.setVisible(true);
        //1-5 设置 窗口大小
        this.setSize(width, height);
        //1-6 设置 窗口居中位置
        this.setLocationRelativeTo(null);
        //1-7 设置游戏窗口是否改变
        this.setResizable(false);
        //1-8 设置窗口标题
        this.setTitle("大鱼吃小鱼");
        //1-9 设置游戏窗口关闭按钮
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //4.启动页面点击事件 开始按钮点击鼠标左键 游戏状态码为1 开始文字消失
        //4-1 为页面添加鼠标点击事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);

                //4-2 用if条件来判断是否是鼠标左键点击事件，并且进入游戏状态
                //4-3 鼠标左键对应数值为1
                if (e.getButton() == 1 && state == 0) {
                    //1表示游戏中
                    state = 1;
                    // repaint()再次调用paint方法绘制窗口
                    repaint();
                 //在游戏胜利的情况下点击鼠标左键重新开始
                } if (e.getButton() == 1 && state == 3) {
                    reGame();
                    state = 0;
                }
            }
        });
        //抬起

        //键盘移动
        this.addKeyListener(new KeyAdapter() {
            @Override//按压
            public void keyPressed(KeyEvent e) {
                super.keyTyped(e);
                //WASD
                if (e.getKeyCode() == 87) {
                    GameUtils.UP = true;
                }
                if (e.getKeyCode() == 83) {
                    GameUtils.DOWN = true;
                }
                if (e.getKeyCode() == 65) {
                    GameUtils.LEFT = true;
                }
                if (e.getKeyCode() == 68) {
                    GameUtils.RIGHT = true;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
                if (e.getKeyCode() == 87) {
                    GameUtils.UP = false;
                }
                if (e.getKeyCode() == 83) {
                    GameUtils.DOWN = false;
                }
                if (e.getKeyCode() == 65) {
                    GameUtils.LEFT = false;
                }
                if (e.getKeyCode() == 68) {
                    GameUtils.RIGHT = false;
                }
            }
        });


        //5-5 背景图片循环使用，需要重复调用paint方法
        while (true) {
            repaint();
            //快捷键 Ctrl+Alt+T
            //记录重绘次数 ,即paint的调用次数
            time++;
            try {
                Thread.sleep(60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    /*2-3重写paint方法*/
    @Override
    public void paint(Graphics g) {
        //6-2 懒加载模式初始化对象
        offImage = createImage(width, height);
        Graphics gImage = offImage.getGraphics();

//        super.paint(gImage);
        //2-4用画笔g绘制图片
//        g.drawImage(GameUtils.bgImg, 0, 0, null);
        //3-3 定义游戏状态
//        bg.paintSelf(gImage,myFish.level);
        switch (state) {
            //3-4 在游戏状态为0的语句中设置背景图
            case 0:
                reGame();
               /* gImage.drawImage(GameUtils.bgImg, 0, 0, null);
                //3-5 将画笔颜色改为红色
                gImage.setColor(Color.pink);
                //3-6 设置字体样式
                gImage.setFont(new Font("仿宋", Font.BOLD, 50));
                //3-7 为启动页面添加文字
                gImage.drawString("开始", 500, 350);*/
//                GameUtils.draw(gImage, "开始", 500, 350, 50);
                bg.paintSelf(gImage, myFish.level);
                break;
            case 1:
//                GameUtils.draw(gImage, "分数:" + GameUtils.count, 100, 100, 50);
                bg.paintSelf(gImage, myFish.level);
                //5-4 将背景绘制自身的方法，添加到1中
//                bg.paintSelf(gImage);
//                gImage.drawImage(GameUtils.bgImg, 0, 0, null);
                myFish.paintSelf(gImage);
                //8-3 由于在游戏运行中，敌方鱼不断添加，所以将创造的方法添加到游戏状态码1的代码块中
                logic();
                //绘制敌方的速度
                enemy.x += enemy.speed;//定义移动的方向
                for (Enemy enemy : GameUtils.enemyList) {
                    enemy.paintSelf(gImage);
                }


                   /* if(boss.x<0){
                        gImage.setColor(Color.red);
                        gImage.fillRect(boss.x, boss.y,2400, boss.height/30);
                    }*/



                //11-4 通过画笔对象添加颜色、字体、和积分的现实
//                gImage.setColor(Color.pink);
//                gImage.setFont(new Font("仿宋", Font.BOLD, 50));
//                gImage.drawString("分数:"+GameUtils.count, 100, 100);
                //游戏分数大于200时游戏通关
                if (GameUtils.count >= 200) {
                    state = 3;
                }
                //空格键的数值为32
                this.addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                        super.keyPressed(e);
                        if (e.getKeyCode() == 32) {
                            state = 4;
                        }
                    }
                });
                break;
            case 2:
                bg.paintSelf(gImage, myFish.level);
                gImage.drawString("按空格回到开始界面", 350, 450);
                this.addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                        super.keyPressed(e);
                        if (e.getKeyCode() == 32) {
                            reGame();
                            state = 0;
                        }
                    }
                });
                break;
            case 3:
//                GameUtils.draw(gImage, "通关成功", 500, 350, 25);
                bg.paintSelf(gImage, myFish.level);
                myFish.paintSelf(gImage);
                  /*  gImage.drawImage(GameUtils.bgImg, 0, 0, null);
                    gImage.setColor(Color.pink);
                    gImage.setFont(new Font("仿宋", Font.BOLD, 25));
                    gImage.drawString("通关成功", 500, 350);*/
//                gImage.drawString("总分数:" + GameUtils.count, 500, 380);


                break;
            case 4:
                gImage.drawImage(GameUtils.bgImg, 0, 0, null);
                GameUtils.draw(gImage, "暂停", Color.pink, 500, 350, 50);
                gImage.drawString("按空格继续", 420, 450);
                this.addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                        super.keyPressed(e);
                        if (e.getKeyCode() == 32) {
                            state = 1;
                        }
                    }
                });
                break;
            default:
                break;
        }
        //6-3 把内存中绘制好的图片重新绘制到主窗口中
        g.drawImage(offImage, 0, 0, null);

    }

    //8-2创建一个方法用来批量添加敌方鱼
    void logic() {
        //12-2 设置关卡的难度
        //分数小于5,关卡等级为0
        if (GameUtils.count < 5) {
            GameUtils.level = 0;
            myFish.level = 1;

        } else if (GameUtils.count < 50) {
            GameUtils.level = 1;
            myFish.level = 2;

        } else if (GameUtils.count < 100) {
            GameUtils.level = 2;
            myFish.level = 3;

        } else if (GameUtils.count < 200) {
            GameUtils.level = 3;
            myFish.level = 4;

        }
        random = Math.random();
        //敌方鱼根据不同难度生产不同种类的鱼
        switch (GameUtils.level) {
            //添加敌方boss鱼的生成条件
            case 3:
                isBoss=true;
                if (time % 170 == 0) {
                    boss = new Enemy(Toolkit.getDefaultToolkit().createImage(
                            "images/enemyFish/boss.gif"),
                            -20, (int) (Math.random() * 400 + 80),
                            200, 200, 10, 1, 5, 10);
                    GameUtils.enemyList.add(boss);
                }
            case 2:
                if (time % 110 == 0) {
                    if (random > 0.5) {
                        GameUtils.enemyList.add(new Enemy(Toolkit.getDefaultToolkit().createImage(
                                "images/enemyFish/fish3_r.png"),
                                -20, (int) (Math.random() * 500 + 80),
                                160, 70, 10, 1, 3, 9));
                    } else {
                        GameUtils.enemyList.add(new Enemy(Toolkit.getDefaultToolkit().createImage(
                                "images/enemyFish/fish3_l.png"),
                                1024, (int) (Math.random() * 500 + 80),
                                160, 70, 10, -1, 3, 9));
                    }
                }
            case 1:
                if (time % 90 == 0) {
                    if (random > 0.5) {
                        GameUtils.enemyList.add(new Enemy(Toolkit.getDefaultToolkit().createImage(
                                "images/enemyFish/fish2_r.png"),
                                -20, (int) (Math.random() * 660 + 80),
                                100, 60, 10, 1, 2, 10));
                    } else {
                        //14-1 生成右侧敌方鱼类
                        GameUtils.enemyList.add(new Enemy(Toolkit.getDefaultToolkit().createImage(
                                "images/enemyFish/fish2_l.png"),
                                1024, (int) (Math.random() * 660 + 80),
                                100, 60, 10, -1, 2, 10));
                    }

                }
            case 0:
                //敌方鱼的生成 每调用paint方法10次生成一条鱼
                if (time % 50 == 0) {
                    //左侧敌方鱼和右侧敌方鱼有一半的概率从左右方向出来
                    if (random > 0.5) {
                        enemy = new Enemy.Enemy_1_r();
                    } else {
                    }
                    Enemy.Enemy_1_r enemy1 = new Enemy.Enemy_1_r();
                    //8-3将敌方鱼类添加到敌方物体的集合中
                    GameUtils.enemyList.add(enemy);
                    enemy1.dir = -1;
                    enemy1.x = 1024;
                    enemy1.img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish1_l.gif");
                    GameUtils.enemyList.add(enemy1);
                    //14-3 将敌方右类添加到敌方集合
//            GameUtils.enemyList.add(new Enemy.Enemy_1_r.Enemy_1_l());
//            GameUtils.enemyList.add(new Enemy(Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish2_r.png"),-1,(int)(Math.random()*400+80),70,50,8,1,1,1));
//            GameUtils.enemyList.add(new Enemy(Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish3_r.png"),-1,(int)(Math.random()*400+80),70,50,10,1,1,1));
                }
                break;
            default:
                break;
        }
        for (Enemy enemy : GameUtils.enemyList) {
            //定义敌方鱼类移动的方向
            if(enemy.count==10){
                boss=enemy;
                boss.x = boss.x + boss.dir * boss.speed;
            }
            enemy.x = enemy.x + enemy.dir * enemy.speed;
            //10 我方鱼与敌方鱼的碰撞检测
            if (myFish.getRect().intersects(enemy.getRect())) {
                if (myFish.level>=enemy.type) {
                    //把敌方鱼进行移除操作
                    enemy.x = -200;
                    enemy.y = -200;
//                myFish.width += 2;
//                myFish.height += 2;
                    //11-3 当我方鱼与敌方鱼发生碰撞检测完成后加积分
                    GameUtils.count = GameUtils.count + enemy.count;
                }else {
                    state=2;
                }
            }


        }
    }
    //重新开始的方法
    public void  reGame(){
        //敌方鱼清零 我方鱼大小 等级 坐标 关卡等级初始化 分数为0
        state=0;
        myFish.count=0;
        myFish.level=1;
        isBoss=false;
        myFish.x=700;
        myFish.y=500;
        myFish.width=60;
        myFish.height=60;
        time=0;
        GameUtils.enemyList.clear();
        GameUtils.level=0;

    }
    //1-10 创建游戏启动方法，查看结果
    public static void main(String[] args) {
        GameWin gameWin = new GameWin();

        gameWin.launch();

    }

}
